package com.kanche.bigbaby.collection.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.TextView;

import com.kanche.bigbaby.R;
import com.kanche.bigbaby.collection.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

//import com.kkche.bss.R;
//import com.kkche.bss.utils.StringUtils;

//import com.kkche.merchant.R;
//import com.kkche.merchant.adpaters.KeyValueSpinnerAdapter;
//import com.kkche.merchant.utils.StringUtil;

/**
 * Created by nealmi on 6/11/14.
 */
public class ItemSpinnerConfig extends ItemConfig implements AdapterView.OnItemSelectedListener {
    private int layoutId = R.layout.input_list_item_spinner;
    private List<Map<String, String>> spinnerData = new ArrayList<Map<String, String>>();

    public ItemSpinnerConfig(String key, String value, String label) {
        super(key, value, label);
    }

    public ItemSpinnerConfig(String key, String value, String label, List<Map<String, String>> data) {
        super(key, value, label);
        this.spinnerData = data;
    }


    @Override
    public View getView(View convertView, Context mContext) {
        Holder holder = null;
        String canonicalName = ItemSpinnerConfig.class.getCanonicalName();
//        if (convertView != null)
//            holder = (Holder) convertView.getTag();
//        if (convertView == null || !canonicalName.equals(holder.type)) {
        holder = new Holder();

        LayoutInflater inflater = LayoutInflater.from(mContext);

        convertView = inflater.inflate(layoutId, null);
        holder.spinner = (Spinner) convertView.findViewById(R.id.spinner);
        holder.spinner.setOnItemSelectedListener(this);
        holder.label = (TextView) convertView.findViewById(R.id.label);
        holder.type = canonicalName;
//            convertView.setTag(holder);
//        }
        holder.label.setText(getLabel());
        holder.spinner.setAdapter(new KeyValueSpinnerAdapter(spinnerData, mContext));
        String value = getValue();
        if (StringUtils.hasText(value)) {
            if (spinnerData != null) {
                for (int i = 0; i < spinnerData.size(); i++) {
                    if (value.equals(spinnerData.get(i).get("key"))) {
                        holder.spinner.setSelection(i);
                        break;
                    }
                }
            }
        } else {
        }
        if (holder.caption != null)
            holder.caption.setText(getCaption());
        if (StringUtils.hasText(getErrorMessage())) {
            holder.spinner.setPrompt(getErrorMessage());
        } else {
            holder.spinner.setPrompt("");
        }
        return convertView;
    }

    class Holder {
        String type;
        TextView caption, label;
        Spinner spinner;
    }

    public List<Map<String, String>> getSpinnerData() {
        return spinnerData;
    }

    public void setSpinnerData(List<Map<String, String>> spinnerData) {
        this.spinnerData = spinnerData;
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        Map<String, String> kv = (Map<String, String>) adapterView.getItemAtPosition(i);
        if (kv != null) setValue(kv.get("key"));
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
        setValue("");
    }
}
